package com.zyc.app.woa.utils;

import android.content.res.Configuration;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;
import com.zyc.app.woa.WoaApp;
import com.zyc.app.woa.WxConfig;
import org.apache.commons.io.FileUtils;

import java.io.File;

/**
 * Author: yingchang.zhang
 * Date  : 2014/11/26
 */
public class WxUtils {
    public static final String PROFILE_BASE_URL = "http://weixin.sogou.com/gzhwap";
    public static final String SEARCH_BASE_URL = "http://weixin.sogou.com/weixinwap";
    public static final String SEARCH_URL = SEARCH_BASE_URL + "?ie=utf8&query=%s&type=1";

    public static String buildSearchUrl(String keyWord) {
        return String.format(SEARCH_URL, keyWord);
    }

    public static void initWebCacheSettings(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
//        settings.setDomStorageEnabled(true);
//        settings.setDatabaseEnabled(true);
//        settings.setAppCacheEnabled(true);
//        settings.setAppCachePath(WxConfig.CACHE_ROOT_PATH);
    }

    public static void clearWebCaches() {
        FileUtils.deleteQuietly(new File(WxConfig.CACHE_ROOT_PATH + File.separator + "webviewCache"));
    }

    public static void toast(int text) {
        toast(WoaApp.getApp().getResources().getString(text));
    }

    public static void toast(String text) {
        Toast.makeText(WoaApp.getApp(), text, Toast.LENGTH_SHORT).show();
    }

    public static boolean isTablet() {
        int screenSize = WoaApp.getApp().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
        Trace.log("screen layout size: " + screenSize);
        return screenSize >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }
}
